package com.frame.assist.zip;

import java.io.*;

/**
 * @Title: zip加压与压缩
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company:
 * @author 王海锋
 * @CreatedTime:2012-3-30 上午09:53:16
 * @version 1.0
 */

public class FileZip {

    /**
     * zip解压
     * 功能:<br>
     * 参数:zipFile为zip文件路径，toPath为加压到哪个目录<br>
     * 作者:王海锋<br>
     * 时间:2012-3-30 上午09:55:22<br>
     * 说明:<br>
     * 变更:<br>
     * @throws Exception
     */
    public void unzip(String zipFile,String toPath) throws Exception{
        InputStream in=null;
        try{
            in=new FileInputStream(new File(zipFile));
            ZipInputStream zipIn=new ZipInputStream(in);
            ZipEntry entry = null;
            while((entry = zipIn.getNextEntry()) != null)
            {
                if(entry.isDirectory())
                    new File(toPath+entry.getName()).mkdirs();
                else{
                    String eName=entry.getName();
                    int n=eName.lastIndexOf(".");
                    File pfile=new File(toPath+eName).getParentFile();
                    if(!pfile.exists())
                        pfile.mkdirs();
                    FileOutputStream fos = null;
                    try{
                        fos = new FileOutputStream(toPath+eName);
                        byte[] temp = new byte[8 * 1024];
                        int l;
                        while((l = zipIn.read(temp)) > 0)
                        {
                            fos.write(temp,0,l);
                        }

                    }catch(IOException e){e.printStackTrace();}finally{
                        if(fos!=null)
                            try
                            {
                                fos.close();
                            }
                            catch(Throwable ex)
                            {
                            }
                    }
                }
            }
            zipIn.closeEntry();
        }catch(Exception e){
            throw e;
        }finally{
            if(in!=null)
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
    public void unzip(InputStream in,String toPath) throws Exception{
        try{
            ZipInputStream zipIn=new ZipInputStream(in);
            ZipEntry entry = null;
            while((entry = zipIn.getNextEntry()) != null)
            {
                if(entry.isDirectory())
                    new File(toPath+entry.getName()).mkdirs();
                else{
                    String eName=entry.getName();
                    int n=eName.lastIndexOf(".");
                    File pfile=new File(toPath+eName).getParentFile();
                    if(!pfile.exists())
                        pfile.mkdirs();
                    FileOutputStream fos = null;
                    try{
                        fos = new FileOutputStream(toPath+eName);
                        byte[] temp = new byte[8 * 1024];
                        int l;
                        while((l = zipIn.read(temp)) > 0)
                        {
                            fos.write(temp,0,l);
                        }

                    }catch(IOException e){e.printStackTrace();}finally{
                        if(fos!=null)
                            try
                            {
                                fos.close();
                            }
                            catch(Throwable ex)
                            {
                            }
                    }
                }
            }
            zipIn.closeEntry();
        }catch(Exception e){
            throw e;
        }finally{
            if(in!=null)
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
    /**
     *
     * 功能:压缩文件<br>
     * 参数:file要压缩的文件；fileName压缩后的文件名；directory压缩到哪个目录下<br>
     * 作者:王海锋<br>
     * 时间:2013-4-15 上午11:32:56<br>
     * 说明:<br>
     * 变更:<br>
     */
    public void zip(ZipOutputStream outzip,File file,String fileName,String directory) throws Exception{
        String path=file.getName();
        if(fileName!=null)
            path=fileName;
        if(directory!=null && !"".equals(directory))
            path=directory+"/"+path;
        ZipEntry entry = new ZipEntry(fileName);
        outzip.putNextEntry(entry);
        FileInputStream in=null;
        try{
            in=new FileInputStream(file);
            byte[] temp = new byte[1024*2];
            int l;
            while((l = in.read(temp)) > 0)
            {
                outzip.write(temp,0,l);
            }
        }catch(Exception e){throw e;}finally{
            if(in!=null)
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
}
